<template>
  <div>
    <el-card class="search">
      <el-row>
        <el-col :span="6">
          <div class="divLabel">仓库编号</div>
          <el-input v-model="searchForm.like_code" placeholder="请输入"></el-input>
        </el-col>
        <el-col :span="6">
          <div class="divLabel">仓库名称</div>
          <el-input v-model="searchForm.like_name" placeholder="请输入"></el-input>
        </el-col>
        <el-col :span="6">
          <div class="divLabel">仓库状态</div>
          <el-select v-model="searchForm.status" placeholder="请输入" clearable>
            <el-option label="全部" value="null"></el-option>
            <el-option label="停用" value="0"></el-option>
            <el-option label="启用" value="1"></el-option>
          </el-select>
        </el-col>
        <el-col :span="6">
          <div class="divLabel"></div>
          <el-row type="flex" justify="end">
            <el-button type="primary" round @click="search">搜索</el-button>
            <el-button
              round
              style="border: none; background: #f8f5f5; margin-left: 20px"
              @click="reset"
              >重置</el-button
            >
          </el-row>
        </el-col>
      </el-row>
    </el-card>
    <el-card class="table-container">
      <el-row type="flex" justify="space-between">
        <el-button type="success" class="add" round @click="add">新增仓库</el-button>
        <slot name="button"></slot>
      </el-row>
      <el-row>
        <el-table
          :cell-style="{ borderColor: '#f5efee!important' }"
          size="small"
          :data="tableData"
          border
          stripe
          style="width: 100%"
          :header-cell-style="{
            background: '#f9f6ee',
            borderColor: '#f5efee!important',
            fontSize: '13px',
            fontFamily: 'PingFangSC,PingFangSC-Medium',
            fontWeight: '400',
            color: '#887e7e',
          }"

        >
          <el-table-column type="index" align="center" label="序号" width="68">
          </el-table-column>
          <el-table-column
            prop="code"
            align="center"
            label="仓库编码"
            width="150"
          >
          </el-table-column>
          <el-table-column
            prop="name"
            align="center"
            label="仓库名称"
            width="150"
          >
          </el-table-column>
          <el-table-column
            :formatter="typeFormat"
            prop="type"
            align="center"
            label="仓库类型"
            width="150"
          >
          </el-table-column>
          <el-table-column
            prop="location"
            align="center"
            label="省/市/区"
            width="200"
          >
          </el-table-column>
          <el-table-column
            prop="address"
            align="center"
            label="详细地址"
            width="150"
          >
          </el-table-column>
          <el-table-column
            :formatter="statusFormat"
            prop="status"
            align="center"
            label="仓库状态"
            width="150"
          >
          </el-table-column>
          <el-table-column
            prop="surface"
            align="center"
            label="仓库面积㎡"
            width="150"
          >
          </el-table-column>
          <el-table-column
            prop="includedNum"
            align="center"
            label="库区数量"
            width="150"
          >
          </el-table-column>
          <el-table-column prop="personName" align="center" label="负责人" width="150">
          </el-table-column>
          <el-table-column
            prop="phone"
            align="center"
            label="联系电话"
            width="150"
          >
          </el-table-column>
          <el-table-column
            prop="updateTime"
            align="center"
            label="更新时间"
            width="150"
          >
          </el-table-column>
          <el-table-column
            fixed="right"
            align="center"
            label="操作"
            width="180"
          >
            <template v-slot="{ row }">
              <el-button type="text" size="small" @click="edit(row)">编辑</el-button>
              <el-button type="text" size="small" @click="toggleStatus(row)">{{ row.status === 1 ? '停用' : '启用' }}</el-button>
              <el-button type="text" size="small" @click="del(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-row>
      <el-row type="flex" justify="center" style="margin-top:20px;margin-bottom:34px">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page.current"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="page.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total">
        </el-pagination>
      </el-row>
    </el-card>
    <!-- 启用/停用对话框 -->
    <el-dialog
      :title="status === 1 ? '确认停用' : '确认启用' "
      :visible.sync="dialogVisible"
      width="30%">
      <span>确认要{{ status === 1 ? '停用' : '启用' }}：{{ warehouseName }} 吗？</span>
      <span slot="footer" class="dialog-footer">
        <el-button round @click="dialogVisible = false">取 消</el-button>
        <el-button round type="primary" @click="confirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { delWarehouseData, getWarehouseData, updateWarehouseData } from '@/api/warehouse'
import warehouseEnum from '@/api/constant/table'
export default {
  data () {
    return {
      id: '',
      warehouseName: '',
      status: '',
      dialogVisible: false,
      searchForm: {
        like_code: '', // 仓库编号
        like_name: '', // 仓库名称
        status: '' // 仓库状态
      },
      tableData: [],
      page: {
        current: 1, // 当前页
        size: 10, // 每页数量
        totalPage: 0, // 总页数
        total: 0 // 总数量
      }
    }
  },
  created () {
    this.getTableList()
  },
  methods: {
    // 新增仓库
    add () {
      this.$router.push('/warehouse/details/null')
    },
    // 编辑
    edit (row) {
      console.log(row)
      this.$router.push(`/warehouse/details/${row.id}`)
    },
    // 确定修改
    async confirm () {
      await updateWarehouseData({
        status: this.status === 1 ? 0 : 1,
        id: this.id
      })
      this.$message.success('状态改变成功')
      this.getTableList()
      this.dialogVisible = false
    },
    // 切换状态
    async toggleStatus (row) {
      this.warehouseName = row.name
      this.status = row.status
      this.id = row.id
      this.dialogVisible = true
    },
    // 删除
    async del (id) {
      await delWarehouseData({
        ids: [id]
      })
      this.$message.success('删除成功')
      this.getTableList()
    },
    // 重置
    reset () {
      this.getTableList()
      this.searchForm = {
        like_code: '', // 仓库编号
        like_name: '', // 仓库名称
        status: '', // 仓库状态
        current: this.page.current,
        size: this.page.size
      }
    },
    // 搜索
    async search () {
      const res = await getWarehouseData(this.searchForm)
      console.log(res)
      this.tableData = res.data.records
      this.page.size = +res.data.size
      this.page.total = +res.data.total
      this.page.totalPage = +res.data.pages
    },
    // 获取表格数据
    async getTableList () {
      const res = await getWarehouseData({
        like_code: '',
        like_name: '',
        current: this.page.current,
        size: this.page.size
      })
      this.tableData = res.data.records
      this.page.size = +res.data.size
      this.page.total = +res.data.total
      this.page.totalPage = +res.data.pages
    },
    // 格式化状态
    statusFormat (row, column, cellValue, index) {
      return cellValue === 1 ? '启用' : '停用'
    },
    // 格式化类型
    typeFormat (row, column, cellValue, index) {
      return warehouseEnum.warehouseType.find(item => item.id === cellValue).value
    },
    // 切换页面
    handleCurrentChange (val) {
      this.page.current = val
      this.getTableList()
      console.log(`当前页: ${val}`)
    },
    handleSizeChange (val) {
      this.page.size = val
      this.getTableList()
      console.log(`每页 ${val} 条`)
    }
  }
}
</script>

<style lang="scss" scoped>
.el-select-dropdown__item.selected {
  font-size: 14px;
  padding: 0 20px;
  position: relative;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: #606266;
  height: 34px;
  line-height: 34px;
  box-sizing: border-box;
  cursor: pointer;
  font-weight: normal;
}
.search {
  height: 127px;
  padding: 10px;
  box-sizing: border-box;
  .el-row {
    .el-col {
      .divLabel {
        font-family: PingFangSC, PingFangSC-Medium;
        margin-bottom: 10px;
        height: 18px;
        font-size: 12px;
        color: #887e7e;
      }
      .el-button {
        display: flex;
        justify-content: center;
        align-items: center;
      }
      .el-input::v-deep {
        width: 270px;
        .el-input__inner {
          height: 40px;
          line-height: 40px;
          border: 1px solid #f8f5f5;
          background: #f8f5f5;
          border-radius: 6px;
          padding-left: 12px;
          padding-right: 12px;
          &::placeholder {
            color: #b5abab;
          }
        }
      }
      .el-select::v-deep {
        .el-input {
          width: 270px;
          .el-input__inner {
            height: 40px;
            line-height: 40px;
            border: 1px solid #f8f5f5;
            background: #f8f5f5;
            border-radius: 6px;
            padding-left: 12px;
            padding-right: 12px;
            &::placeholder {
              color: #b5abab;
            }
          }
        }
      }
    }
  }
}
.table-container {
  margin-top: 20px;
  padding: 0;
  ::v-deep .el-card__body {
    padding: 0;
  }
  .add {
    margin-left: 30px;
    margin-top: 20px;
    margin-bottom: 20px;
  }
  .el-table::v-deep {
    .el-table__row--striped {
      td {
        background: #fdfcf9!important;
      }
    }
    .el-table__row.hover-row {
      td {
        background: #fff6e2!important;
      }
    }
  }
}
</style>
